<template>
  <div class="publisher-form">
    <div class="page-header">
      <h2>{{ isEdit ? '编辑出版社' : '添加出版社' }}</h2>
      <el-button @click="goBack">
        <el-icon><ArrowLeft /></el-icon>
        返回列表
      </el-button>
    </div>

    <div class="form-section">
      <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        label-width="100px"
      >
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="出版社名称" prop="name">
              <el-input v-model="formData.name" placeholder="请输入出版社名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="phone">
              <el-input v-model="formData.phone" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="formData.email" placeholder="请输入邮箱" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="地址" prop="address">
          <el-input v-model="formData.address" type="textarea" :rows="3" placeholder="请输入地址" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleSubmit" :loading="submitting">
            {{ isEdit ? '更新' : '添加' }}
          </el-button>
          <el-button @click="handleReset">重置</el-button>
          <el-button @click="goBack">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, computed, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { ArrowLeft } from '@element-plus/icons-vue'
import publisherApi from '@/api/publisher'

const router = useRouter()
const route = useRoute()

const formRef = ref(null)
const isEdit = computed(() => !!route.params.id)
const submitting = ref(false)

const formData = reactive({
  id: null,
  name: '',
  phone: '',
  email: '',
  address: ''
})

const formRules = reactive({
  name: [{ required: true, message: '请输入出版社名称', trigger: 'blur' }],
  phone: [{ required: true, message: '请输入联系电话', trigger: 'blur' }]
})

const goBack = () => router.push('/publisher')

const handleReset = () => {
  formRef.value?.resetFields()
}

const getPublisherDetail = async (id) => {
  try {
    const result = await publisherApi.getById(id)
    if (result) {
      Object.assign(formData, result)
    }
  } catch (error) {
    ElMessage.error('获取出版社详情失败: ' + error.message)
    goBack()
  }
}

const handleSubmit = async () => {
  try {
    const valid = await formRef.value?.validate()
    if (!valid) return

    submitting.value = true

    if (isEdit.value) {
      await publisherApi.update(formData)
      ElMessage.success('更新出版社成功')
    } else {
      await publisherApi.add(formData)
      ElMessage.success('添加出版社成功')
    }

    goBack()
  } catch (error) {
    ElMessage.error((isEdit.value ? '更新' : '添加') + '出版社失败: ' + error.message)
  } finally {
    submitting.value = false
  }
}

onMounted(() => {
  if (isEdit.value && route.params.id) {
    getPublisherDetail(route.params.id)
  }
})
</script>

<style scoped>
.publisher-form {
  padding: 20px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.page-header h2 {
  margin: 0;
  color: #303133;
}

.form-section {
  background: #fff;
  padding: 30px;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
</style>
